/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogUsuario.java
 *
 * Created on Dec 21, 2010, 4:16:09 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleConsultor;
import br.com.tbp.controle.ControleUsuario;
import br.com.tbp.exception.InputValidationException;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.model.domain.Consultor;
import br.com.tbp.model.domain.Usuario;
import br.com.tbp.model.domain.constant.CategoriaDeUsuario;
import br.com.tbp.utils.DateUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego Freire
 */
public class UsuarioDialog extends javax.swing.JDialog {
    
    private boolean novoUsuario = true;
    
    public UsuarioDialog(java.awt.Frame parent, boolean modal) {
        super(parent, "TBP - Usuário", modal);
        initComponents();
        setLocationRelativeTo(null);
        dtCadastroTextField.setText(DateUtils.dataObjetoParaString(new Date()));
        try {
            usuarioIdTextField.setText("" + new ControleUsuario().maxUsuarioId());
        } catch (PersistenciaException pex) {
            usuarioIdTextField.setText("0");
        }
        categoriaComboBox.setSelectedIndex(-1);
        consultorComboBox.setSelectedIndex(-1);
    }
    
    public UsuarioDialog(java.awt.Frame parent, boolean modal, long usuarioId) {
        super(parent, "TBP - Usuário", modal);
        initComponents();
        setLocationRelativeTo(null);
        montarUsuario(usuarioId);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        salvarButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        usuarioIdTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nomeTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        emailTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dtCadastroTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        ativoRadioButton = new javax.swing.JRadioButton();
        desativadoTextField = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        loginTextField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        senhaTextField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        categoriaComboBox = new javax.swing.JComboBox();
        consultorLabel = new javax.swing.JLabel();
        consultorComboBox = new javax.swing.JComboBox();

        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        salvarButton.setMnemonic('s');
        salvarButton.setText("Salvar");
        salvarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarButtonActionPerformed(evt);
            }
        });

        fecharButton.setMnemonic('f');
        fecharButton.setText("Fechar (ESC)");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Info. Básicas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12))); // NOI18N

        jLabel1.setText("Usuário Id:");

        usuarioIdTextField.setEditable(false);
        usuarioIdTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        usuarioIdTextField.setText("001");
        usuarioIdTextField.setFocusable(false);

        jLabel2.setText("Nome:");

        jLabel4.setText("E-mail:");

        jLabel5.setText("Dt. Cadastro:");

        dtCadastroTextField.setEditable(false);
        dtCadastroTextField.setFocusable(false);

        jLabel6.setText("Situação:");

        buttonGroup1.add(ativoRadioButton);
        ativoRadioButton.setSelected(true);
        ativoRadioButton.setText("Ativo");

        buttonGroup1.add(desativadoTextField);
        desativadoTextField.setText("Desativado");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usuarioIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addComponent(emailTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addComponent(dtCadastroTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ativoRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(desativadoTextField)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usuarioIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dtCadastroTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(ativoRadioButton)
                    .addComponent(desativadoTextField))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Info. Acesso", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12))); // NOI18N

        jLabel7.setText("Login:");

        jLabel8.setText("Senha:");

        jLabel9.setText("Categoria:");

        categoriaComboBox.setModel(new DefaultComboBoxModel(CategoriaDeUsuario.values()));
        categoriaComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                categoriaComboBoxItemStateChanged(evt);
            }
        });

        consultorLabel.setText("Consultor:");
        consultorLabel.setEnabled(false);

        try{
            consultorComboBox.setModel(new DefaultComboBoxModel(new ControleConsultor().pesquisar2().toArray()));
            consultorComboBox.setEnabled(false);
        }catch(PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(29, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(consultorLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(senhaTextField)
                    .addComponent(categoriaComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(consultorComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(senhaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(categoriaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(consultorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(consultorLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(326, Short.MAX_VALUE)
                .addComponent(salvarButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fecharButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvarButton)
                    .addComponent(fecharButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salvarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarButtonActionPerformed
        if (novoUsuario) {
            long salvo = this.gravarUsuario();
            if (salvo == 1) {
                JOptionPane.showMessageDialog(null, "O usuário foi cadastrado com sucesso");
                this.limparCampos();
            } else {
                return;
            }
        } else {
            byte alterado = this.alterarUsuario();
            if (alterado == 1) {
                JOptionPane.showMessageDialog(null, "Os dados do usuário foram alterados com sucesso");
                nomeTextField.requestFocus();
            } else {
                return;
            }
        }
    }//GEN-LAST:event_salvarButtonActionPerformed
    
    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
    }//GEN-LAST:event_fecharButtonActionPerformed
    
    private void categoriaComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_categoriaComboBoxItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            consultorComboBox.setEnabled(((CategoriaDeUsuario) categoriaComboBox.getSelectedItem()).equals(CategoriaDeUsuario.CONSULTOR));
            consultorLabel.setEnabled(consultorComboBox.isEnabled());
        }
    }//GEN-LAST:event_categoriaComboBoxItemStateChanged
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                UsuarioDialog dialog = new UsuarioDialog(null, true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton ativoRadioButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox categoriaComboBox;
    private javax.swing.JComboBox consultorComboBox;
    private javax.swing.JLabel consultorLabel;
    private javax.swing.JRadioButton desativadoTextField;
    private javax.swing.JTextField dtCadastroTextField;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField loginTextField;
    private javax.swing.JTextField nomeTextField;
    private javax.swing.JButton salvarButton;
    private javax.swing.JTextField senhaTextField;
    private javax.swing.JTextField usuarioIdTextField;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private long gravarUsuario() {
        byte salvo = -1;
        try {
            this.validarEntradaDoUsuario();
            
            Usuario usuario = new Usuario();
            usuario.setNome(nomeTextField.getText());
            usuario.setEmail(emailTextField.getText());
            //usuario.setDt_cadastro(DateUtils.dataStringParaObjeto(dtCadastroTextField.getText()));
            usuario.setDt_cadastro(new Date());
            usuario.setAtivo(ativoRadioButton.isSelected());
            usuario.setLogin(loginTextField.getText());
            usuario.setSenha(senhaTextField.getText());
            usuario.setCategoria((CategoriaDeUsuario) categoriaComboBox.getSelectedItem());
            if (consultorComboBox.isEnabled()) {
                usuario.setConsultor((Consultor) consultorComboBox.getSelectedItem());
            }
            
            ControleUsuario controle = new ControleUsuario();
            controle.adicionar(usuario);
            
            if (usuario.getUsuarioId() > 0) {
                salvo = 1;
            }
        } catch (InputValidationException ivex) {
            JOptionPane.showMessageDialog(null, ivex.getMessage(), "Atenção", JOptionPane.WARNING_MESSAGE);
        } catch (PersistenciaException pex) {
            JOptionPane.showMessageDialog(null, pex.getMessage(), "Problemas", JOptionPane.WARNING_MESSAGE);
        }
        return salvo;
    }
    
    private byte alterarUsuario() {
        byte alterado = -1;
        
        try {
            this.validarEntradaDoUsuario();
            
            Usuario usuario = new Usuario();
            usuario.setUsuarioId(Long.parseLong(usuarioIdTextField.getText()));
            usuario.setNome(nomeTextField.getText());
            usuario.setEmail(emailTextField.getText());
            usuario.setAtivo(ativoRadioButton.isSelected());
            usuario.setLogin(loginTextField.getText());
            usuario.setCategoria((CategoriaDeUsuario) categoriaComboBox.getSelectedItem());
            if (consultorComboBox.isEnabled()) {
                usuario.setConsultor((Consultor) consultorComboBox.getSelectedItem());
            }
            
            ControleUsuario controle = new ControleUsuario();
            int modificou = controle.modificar(usuario);
            if (modificou > 0) {
                alterado = 1;
            }
            
        } catch (InputValidationException ivex) {
            JOptionPane.showMessageDialog(null, ivex.getMessage(), "Atenção", JOptionPane.WARNING_MESSAGE);
        } catch (PersistenciaException pex) {
            JOptionPane.showMessageDialog(null, pex.getMessage(), "Problemas", JOptionPane.WARNING_MESSAGE);
        }
        
        return alterado;
    }
    
    private void limparCampos() {
        try {
            usuarioIdTextField.setText("" + new ControleUsuario().maxUsuarioId());
        } catch (PersistenciaException pex) {
            usuarioIdTextField.setText("0");
        }
        nomeTextField.setText(null);
        emailTextField.setText(null);
        dtCadastroTextField.setText(DateUtils.dataObjetoParaString(new Date()));
        ativoRadioButton.setSelected(true);
        loginTextField.setText(null);
        senhaTextField.setText(null);
        categoriaComboBox.setSelectedIndex(-1);
        nomeTextField.requestFocus();
    }
    
    private void validarEntradaDoUsuario() throws InputValidationException {
        if (nomeTextField.getText().isEmpty()) {
            nomeTextField.requestFocus();
            throw new InputValidationException("O nome deve ser informado");
        } else if (loginTextField.getText().isEmpty()) {
            loginTextField.requestFocus();
            throw new InputValidationException("O login deve ser informado");
        } else if (novoUsuario) {
            if (senhaTextField.getText().isEmpty()) {
                senhaTextField.requestFocus();
                throw new InputValidationException("A senha deve ser informada");
            }
        }
        if (categoriaComboBox.getSelectedItem() == null) {
            categoriaComboBox.requestFocus();
            throw new InputValidationException("A categoria não foi selecionada");
        }
        if((consultorComboBox.isEnabled()) && (consultorComboBox.getSelectedItem() == null)) {
            consultorComboBox.requestFocus();
            throw new InputValidationException("O consultor não foi selecionado");
        }
    }
    
    private String ocultarSenha(int tamanho) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= tamanho; i++) {
            sb.append("*");
        }
        return sb.toString();
    }
    
    private void montarUsuario(long usuarioId) {
        try {
            Usuario usuario = this.buscarUsuario(usuarioId);
            if (usuario != null) {
                usuarioIdTextField.setText("" + usuario.getUsuarioId());
                nomeTextField.setText(usuario.getNome());
                emailTextField.setText(usuario.getEmail());
                dtCadastroTextField.setText(DateUtils.dataObjetoParaString(usuario.getDt_cadastro()));
                if (usuario.isAtivo()) {
                    ativoRadioButton.setSelected(true);
                } else {
                    desativadoTextField.setSelected(true);
                }
                loginTextField.setText(usuario.getLogin());
                senhaTextField.setText(this.ocultarSenha(usuario.getSenha().length()));
                senhaTextField.setEditable(false);
                senhaTextField.setFocusable(false);
                categoriaComboBox.setSelectedItem(usuario.getCategoria());
                if((usuario.getConsultor() != null) && (usuario.getConsultor().getId() > 0)) {
                    consultorComboBox.setSelectedItem(usuario.getConsultor());
                } else {
                    consultorComboBox.setSelectedIndex(-1);
                }
                
                this.novoUsuario = false;
            }
        } catch (PersistenciaException pex) {
            JOptionPane.showMessageDialog(null, pex.getMessage(), "Problemas", JOptionPane.WARNING_MESSAGE);
            System.out.println(pex.getMessage());
        }
    }
    
    private Usuario buscarUsuario(long usuarioId) throws PersistenciaException {
        ControleUsuario controle = new ControleUsuario();
        return controle.get(usuarioId);
    }
    
    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fecharButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
        
        return myRootPane;
    }
}
